package com.gxa.autoparts.portal.service.impl;

import com.gxa.autoparts.core.pojo.AfterImg;
import com.gxa.autoparts.core.pojo.AfterSale;
import com.gxa.autoparts.core.pojo.Order;
import com.gxa.autoparts.core.service.impl.BaseServiceImpl;
import com.gxa.autoparts.portal.mapper.AfterSaleMapper;
import com.gxa.autoparts.portal.service.AfterImgService;
import com.gxa.autoparts.portal.service.AfterSaleService;
import com.gxa.autoparts.portal.service.MyOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("afterSaleService")
public class AfterSaleServiceImpl extends BaseServiceImpl<AfterSale> implements AfterSaleService {
    @Autowired
    AfterSaleMapper afterSaleMapper;
    @Autowired
    MyOrderService myOrderService;
    @Autowired
    AfterImgService afterImgService;
    @Override
    public List<AfterSale> checkMyAfterSale(Long userId) {
        List<AfterSale> afterSales = afterSaleMapper.allAfterSaleList(userId);
        for (AfterSale afterSale : afterSales) {
            List<AfterImg> afterImgs = afterSaleMapper.afterImgList(userId, afterSale.getId());
            for (AfterImg afterImg : afterImgs) {
                afterSale.getAfterImgList().add(afterImg.getImg());
            }
            List<Order> orders = myOrderService.checkMyOrder(userId);
            for (Order order : orders) {
                if (order.getId() == afterSale.getOrderId()){
                    afterSale.setOrder(order);
                }
            }
        }
        return afterSales;
    }

    @Override
    public void addAfterSale(AfterSale afterSale) {
        this.insert(afterSale);
        List<String> afterImgList = afterSale.getAfterImgList();
        for (String s : afterImgList) {
            AfterImg afterImg = new AfterImg();
            afterImg.setAfterSaleId(afterSale.getId());
            afterImg.setImg(s);
            afterImgService.insert(afterImg);
        }
    }
}
